module libeventd.Buffer;


import
	libeventd.Types ;

enum{
	EVBUFFER_MMAP		=0x0001,
	EVBUFFER_SENDFILE	=0x0002,
	EVBUFFER_REFERENCE	=0x0004,
	EVBUFFER_IMMUTABLE	=0x0008,
	EVBUFFER_MEM_PINNED_R=0x0010,
	EVBUFFER_MEM_PINNED_W=0x0020,
	EVBUFFER_MEM_PINNED_ANY = EVBUFFER_MEM_PINNED_R|EVBUFFER_MEM_PINNED_W ,
	EVBUFFER_DANGLING	=0x0040,
}
enum evbuffer_eol_style {
	EVBUFFER_EOL_ANY,
	EVBUFFER_EOL_CRLF,
	EVBUFFER_EOL_CRLF_STRICT,
	EVBUFFER_EOL_LF
}

enum evbuffer_ptr_how {
	EVBUFFER_PTR_SET,
	EVBUFFER_PTR_ADD
}

const MAP_FAILED	= cast(void*) -1 ;
const EVBUFFER_CB_ENABLED = 1;

extern(C):

alias void function(evbuffer*, evbuffer_cb_info*, void*) evbuffer_cb_func;
alias void  function( evbuffer*,size_t,size_t,void*) evbuffer_cb ; 
alias void function(void*, size_t, void *) evbuffer_ref_cleanup_cb;


version(USE_SENDFILE){
	static int use_sendfile = 1;
}

version(_EVENT_HAVE_MMAP){
	static int use_mmap = 1;
}

struct evbuffer_ptr {
	ev_ssize_t pos ;
	struct {
		void*	chain;
		size_t	pos_in_chain;
	} //_internal;
}

struct evbuffer_iovec {
	void*	iov_base;
	size_t	iov_len;
}

struct evbuffer_cb_info {
	size_t	orig_size;
	size_t	n_added;
	size_t	n_deleted;
}

evbuffer* evbuffer_new();
void evbuffer_free( evbuffer *buf);
ev_int evbuffer_enable_locking( evbuffer *buf, void *lock);
void evbuffer_lock( evbuffer *buf);
void evbuffer_unlock( evbuffer *buf);
size_t evbuffer_get_length(  evbuffer *buf);
size_t evbuffer_get_contiguous_space(  evbuffer *buf);
ev_int evbuffer_expand( evbuffer *buf, size_t datlen);
ev_int evbuffer_reserve_space( evbuffer *buf, ev_ssize_t size,  evbuffer_iovec *vec, ev_int n_vecs);
ev_int evbuffer_commit_space( evbuffer *buf,   evbuffer_iovec *vec, ev_int n_vecs);
ev_int evbuffer_add( evbuffer *buf,  void *data, size_t datlen);
ev_int evbuffer_remove( evbuffer *buf, void *data, size_t datlen);
ev_ssize_t evbuffer_copyout( evbuffer *buf, void *data_out, size_t datlen);
ev_int evbuffer_remove_buffer( evbuffer *src,  evbuffer *dst, size_t datlen);
char *evbuffer_readln( evbuffer *buffer, size_t *n_read_out,  evbuffer_eol_style eol_style);
ev_int evbuffer_add_buffer( evbuffer *outbuf,  evbuffer *inbuf);
ev_int evbuffer_add_reference( evbuffer *outbuf,  void *data, size_t datlen, evbuffer_ref_cleanup_cb cleanupfn, void *extra);
ev_int evbuffer_add_file( evbuffer *output, ev_int fd, off_t offset, off_t length);
ev_int evbuffer_add_printf( evbuffer *buf,  char *fmt, ...) ;
ev_int evbuffer_add_vprintf( evbuffer *buf,  char *fmt, /* va_list */ ... );
ev_int evbuffer_drain( evbuffer *buf, size_t len);
ev_int evbuffer_write( evbuffer *buffer, evutil_socket_t fd);
ev_int evbuffer_write_atmost( evbuffer *buffer, evutil_socket_t fd, ev_ssize_t howmuch);
ev_int evbuffer_read( evbuffer *buffer, evutil_socket_t fd, ev_int howmuch);
evbuffer_ptr evbuffer_search( evbuffer *buffer,  char *what, size_t len,   evbuffer_ptr *start);
evbuffer_ptr evbuffer_search_range( evbuffer *buffer,  char *what, size_t len,   evbuffer_ptr *start,   evbuffer_ptr *end);
ev_int evbuffer_ptr_set( evbuffer *buffer,  evbuffer_ptr *pos, size_t position, evbuffer_ptr_how how);
evbuffer_ptr evbuffer_search_eol( evbuffer *buffer,   evbuffer_ptr *start, size_t *eol_len_out,  evbuffer_eol_style eol_style);
ev_int evbuffer_peek( evbuffer *buffer, ev_ssize_t len,  evbuffer_ptr *start_at,  evbuffer_iovec *vec_out, ev_int n_vec);
evbuffer_cb_entry *evbuffer_add_cb( evbuffer *buffer, evbuffer_cb_func cb, void *cbarg);
ev_int evbuffer_remove_cb_entry( evbuffer *buffer,  evbuffer_cb_entry *ent);
ev_int evbuffer_remove_cb( evbuffer *buffer, evbuffer_cb_func cb, void *cbarg);
ev_int evbuffer_cb_set_flags( evbuffer *buffer,  evbuffer_cb_entry *cb, ev_uint32_t flags);
ev_int evbuffer_cb_clear_flags( evbuffer *buffer,  evbuffer_cb_entry *cb, ev_uint32_t flags);
ubyte *evbuffer_pullup( evbuffer *buf, ev_ssize_t size);
ev_int evbuffer_prepend( evbuffer *buf,  void *data, size_t size);
ev_int evbuffer_prepend_buffer( evbuffer *dst,  evbuffer* src);
ev_int evbuffer_freeze( evbuffer *buf, ev_int at_front);
ev_int evbuffer_unfreeze( evbuffer *buf, ev_int at_front);
ev_int evbuffer_defer_callbacks( evbuffer *buffer,  event_base *base);


/* compat */
char *evbuffer_readline(evbuffer *buffer);
void evbuffer_setcb(evbuffer *buffer, evbuffer_cb cb, void *cbarg);
ubyte *evbuffer_find(evbuffer *buffer, ubyte *what, size_t len);
